﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CMS.BLL;

public partial class Controls_Products_SaleOffMenu : System.Web.UI.UserControl
{
    private int nCurrentLang = Support.GetCurrentLanguage();
    private int nDefaultLanguage = Support.GetDefaultLanguage();
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            this.InitData();
        }
    }

    private void InitData()
    {
        //int nCat = 0;
        //int Gallery = 1;
        int nPageCount = 0;
        int nPageSize = 6;
        int nStartSaleOff = 0;
        int nExpSaleOff = 0;
        int nPercent = 0;
        int isSaleOff = 1;
        double bPriceFrom = 0;
        double bPriceTo = 10000000;
        int nMaterial = 0;
        int nTradeMark = 0;
        int nItemID = 0;
        int nItemView = 6;
        Products objProduct = new Products();
        DataTable dtbProduct = new DataTable ();
        dtbProduct = objProduct.Search(nItemView, "", "", nItemID, "",nTradeMark, nMaterial, bPriceFrom, bPriceTo, isSaleOff, -1, EnumeType.ACTIVE, nPercent, nExpSaleOff, nStartSaleOff, nDefaultLanguage, nCurrentLang, 1, nPageSize, ref nPageCount);
        rptProductDiscount.DataSource = dtbProduct;
        int nCount = dtbProduct.Rows.Count; 
        rptProductDiscount.DataBind();
    }
    protected void rptProductDiscount_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            ImageButton btnImage = (ImageButton)e.Item.FindControl("btnImage");
            HyperLink lnkProductName = (HyperLink)e.Item.FindControl("lnkProductName");
            Label lblProductCode = (Label)e.Item.FindControl("lblProductCode");
            Label lblPrice = (Label)e.Item.FindControl("lblPrice");
            //Label lblColor = (Label)e.Item.FindControl("lblColor");
            Label lblDiscountPrice = (Label)e.Item.FindControl("lblDiscountPrice");
            ImageButton lnkAddtoCart = (ImageButton)e.Item.FindControl("lnkAddtoCart");

            int nID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "ProductID"));
            lnkProductName.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductName"));
            lblProductCode.Text = "Mã hàng:" + " " + ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductCode"));
            double dPrice = ConvertData.ConvertToDouble(DataBinder.Eval(e.Item.DataItem, "Price"));
             double dPriceSalesOff = ConvertData.ConvertToDouble(DataBinder.Eval(e.Item.DataItem, "PriceSaleOff"));
            if (dPrice > 0)
            { lblPrice.Text = "Giá:" + " " + ConvertData.ConvertToString(Support.FormatCurrency(DataBinder.Eval(e.Item.DataItem, "Price"))) + " " + "₫";
            lblPrice.Visible = true;
            }
            else
            { //lblPrice.Text = "Giá: Liên hệ";
                lblPrice.Visible = false ;
            }
            if (dPriceSalesOff >0)
                {lblDiscountPrice.Text = "Giá KM:" + " " + ConvertData.ConvertToString(Support.FormatCurrency(DataBinder.Eval(e.Item.DataItem, "PriceSaleOff"))) + " " + "₫";}
            else
            {lblDiscountPrice.Text = "Giá KM: Liên hệ" ;}
            
            //lblColor.Text = "Màu:" + " " + this.ConvertColors(ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Colors")));
            lnkProductName.NavigateUrl = Config.ROOT + Pages.FrontEnds.PRODUCT_DETAIL + "?pid=" + nID;
            lnkAddtoCart.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductID"));
            btnImage.ImageUrl = Config.ROOT + "Images/Upload/Products/" + ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ImageDefault"));
            btnImage.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductID"));
        }
    }
    protected void rptProductDiscount_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string strCommand = e.CommandName;
        int nID = ConvertData.ConvertToInt(e.CommandArgument);

        switch (strCommand)
        {
            case "image":
                string sDetail = Config.ROOT + Pages.FrontEnds.PRODUCT_DETAIL + "?pid=" + nID;
                Response.Redirect(sDetail);
                break;
            case "cart":
                // Add product to Cart
                clsBasket objBasket = clsBasket.getclsBaskets(Context);
                ItemclsBaskets item = new ItemclsBaskets(nID);
                objBasket.AddItem(item);
                Response.Redirect(this.GetCurrentPageName());
                break;
        }
    }

    private string ConvertColors(string sColor)
    {
        string sColorDetail = "";
        int nCurrentLang = Support.GetCurrentLanguage();
        if (sColor.Length > 0)
        {
            string[] sColorID = sColor.Split(',');
            int nTotalColor = sColorID.Length;

            for (int y = 1; y < nTotalColor; y++)
            {
                ProductColor objColor = new ProductColor();
                objColor.LoadById(ConvertData.ConvertToInt(sColorID[y]));
                switch (nCurrentLang)
                {
                    case 1:
                        sColorDetail = sColorDetail + objColor.Data.ColorName1 + ",";
                        break;
                    case 2:
                        sColorDetail = sColorDetail + objColor.Data.ColorName2 + ",";
                        break;
                }

            }
            if (sColorDetail.Length > 0)
                sColorDetail = ConvertData.TruncateString(sColorDetail, sColorDetail.Length - 1);
        }
        return sColorDetail;
    }
    public string GetCurrentPageName()
    {
        string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
        System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
        string sRet = oInfo.Name;
        return sRet;
    } 
}